<?php
namespace app\common\service\wechat;

use think\facade\Config;
use WeChatPay\Builder;
use WeChatPay\Crypto\Rsa;
use WeChatPay\Util\PemUtil;

class WechatPay
{
    public static function v3Client()
    {
        $cfg = Config::get('wechat');

        $merchantPrivateKey = Rsa::from('file://' . $cfg['private_key'], Rsa::KEY_TYPE_PRIVATE);
        $platformPublicKey  = Rsa::from('file://' . $cfg['platform_cert'], Rsa::KEY_TYPE_PUBLIC);
        $platformSerial     = PemUtil::parseCertificateSerialNo('file://' . $cfg['platform_cert']);

        return Builder::factory([
            'mchid'      => $cfg['mch_id'],
            'serial'     => $cfg['serial'],
            'privateKey' => $merchantPrivateKey,
            'certs'      => [$platformSerial => $platformPublicKey],
            'base_uri'   => 'https://api.mch.weixin.qq.com/',
        ]);
    }
}